#!/bin/tcsh -fe
####
#
#############################################################################
#                                                                           #
# Title: Repair Project Links                                               #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 12/22/2010                                             #
# Last Modification: 12/22/2010                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 55
#
# MANUAL: <B>Repair Project Links</B>
#
# MANUAL: This script will verify the presence of correctly linked 2d_master.cfg files in this project.
# MANUAL: With the switch you can decide if you want to correct the links.
#
# DISPLAY: SCRATCH_DISK
# DISPLAY: repair_links
#
#$end_local_vars
#
# Static directory variables at disposition are:
# appDir_2dx
# scripts-standard_2dx
# scripts-custom_2dx
#
set bin_2dx = ""
set proc_2dx = ""
set app_2dx_image = ""
#
# Variables to be available from the config file are:
#
set repair_links = ""
#
#$end_vars
#
set scriptname = 2dx_repair_links
#
\rm -f LOGS/${scriptname}.results
#
echo "<<@progress: 1>>"
# echo "<<@evaluate>>"
#
source ${proc_2dx}/2dx_makedirs
#
echo "# IMAGE: 2dx_merge_dirfile.dat <TXT: Directory List>" >> LOGS/${scriptname}.results
#
# This memorizes the current merge directory under the variable "olddir":
set olddir = $PWD
#
echo "::The current working directory is" ${olddir}
echo ": "
#
set linkok = "y"
cd ..
echo "::Checking link in project root director ${PWD}:"
if ( ! -e 2dx_master.cfg ) then
  echo ":: "
  echo "::WARNING: 2dx_master.cfg in project root directory is missing."
  echo ":: "
  set linkok = "n"
else
  \ls -l 2dx_master.cfg
  if ( ! -l 2dx_master.cfg ) then
    echo ":: "
    echo "::WARNING: 2dx_master.cfg in project root directory is not a link."
    echo ":: "
    set linkok = "n"
  endif
endif
echo "<<@progress: +10>>"
if ( ${linkok} == 'y' ) then
  echo "::Root link is ok."
else
  if ( ${repair_links} == "yes" ) then
    ${proc_2dx}/linblock "Repairing link in project root directory ${PWD}"
    \rm -f 2dx_master.cfg
    \ln -s merge/2dx_merge.cfg 2dx_master.cfg
    \ls -l 2dx_master.cfg
  endif
endif
cd ${olddir}
cd ..
#
echo "<<@progress: +10>>"
#
foreach dirfile ( * ) 
  if ( -d ${dirfile} ) then
    if ( ${dirfile} == "merge" ) then
      echo "Not processing merge directory"
    else
      if ( ${dirfile} == "TIFF" ) then
        echo "Not processing TIFF directory"
      else
        cd ${dirfile}
        set herelinkok = "y"
        if ( ! -e 2dx_master.cfg ) then
          ${proc_2dx}/linblock "WARNING: 2dx_master.cfg is missing in ${dirfile}."
          set herelinkok = "n"
          set linkok = "n"
        else
          \ls -l 2dx_master.cfg
          if ( ! -l 2dx_master.cfg ) then
           ${proc_2dx}/linblock "::WARNING: 2dx_master.cfg is not a link in ${dirfile}."
            set herelinkok = "n"
            set linkok = "n"
          endif
        endif
        if( ( ${herelinkok} != "y" ) && ( ${repair_links} == "yes" ) ) then
          echo "::Creating 2dx_master.cfg link in ${dirfile}"
          \rm -f 2dx_master.cfg
          \ln -s ../2dx_master.cfg .
        endif
        cd ..
      endif
      cd ${olddir}/..
    endif
  endif
end
#
echo "<<@progress: +10>>"
#
if ( ${linkok} == 'n' ) then
  if ( ${repair_links} == "yes" ) then
    ${proc_2dx}/linblock "Now refreshing the directory list."
    echo "<<@evaluate>>"
  else
    echo ":: "
    ${proc_2dx}/linblock "Links are not ok. They need to be repaired."
    ${proc_2dx}/linblock "Set the switch to yes, and run this script again."
    echo ":: "
  endif
else
  ${proc_2dx}/linblock "All links are ok."
endif
#
${proc_2dx}/linblock "Done."
echo "<<@progress: 100>>"
#
